package com.yun.routing.direct;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import com.yun.utils.ConnectionUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class ConsumeB {
    private static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws IOException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME,"direct");

        String queueName = channel.queueDeclare().getQueue();

        channel.queueBind(queueName,EXCHANGE_NAME,"error");

        DeliverCallback deliverCallback = (tag,delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println("Receiver :"+delivery.getEnvelope().getRoutingKey()+":"+message);
        };
        channel.basicConsume(queueName,true,deliverCallback,tag ->{});
    }
}
